package com.juyu.miliao.api.controller;

import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.juyu.miliao.api.util.OSSAssumeRole;
import com.juyu.miliao.common.util.UpLoadFile;
import com.juyu.miliao.common.util.oss.OSSConfig;

import net.sf.json.JSONObject;

@MultipartConfig(maxFileSize = 52428800,// 上传文件大小
			 	maxRequestSize = 52428800)  //请求大小
@RestController
public class UploadFileController {
	private final Logger logger = LoggerFactory.getLogger(UploadFileController.class);
	@Autowired
	private OSSConfig ossConfig;
//	/**
//	 * 上传
//	 */
//	@PostMapping("/file")
//	public JSONObject file(HttpServletResponse response, @RequestParam("uploadFile") Part uploadFile) throws IOException {
//		JSONObject json = new JSONObject();
//		String fileUrl = null;
//		try {
//			fileUrl = UpLoadFile.INSTANCE.uploadClassifyImg2WSCDN(ossConfig, uploadFile);
//			json.put("fileUrl", fileUrl);
//		} catch (Exception e) {
//			logger.error("图片上传错误",e);
//			json.put("error", e);
//			return json;
//		}
//		return json;
//	}
	
	@GetMapping("/ossKey")
	public JSONObject ossKey(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		JSONObject json = new JSONObject();
		try {
			final AssumeRoleResponse stsResponse = OSSAssumeRole.assumeRole();
			Map<String, String> respMap = new LinkedHashMap<String, String>();
			respMap.put("StatusCode", "200");
			respMap.put("AccessKeyId", stsResponse.getCredentials().getAccessKeyId());
			respMap.put("AccessKeySecret", stsResponse.getCredentials().getAccessKeySecret());
			respMap.put("SecurityToken", stsResponse.getCredentials().getSecurityToken());
			respMap.put("Expiration", stsResponse.getCredentials().getExpiration());
			respMap.put("endpoint", ossConfig.getEndpointAndroid());
			respMap.put("bucket-name", ossConfig.getBucketName());
			respMap.put("access-url", ossConfig.getAccessUrl());
			json = JSONObject.fromObject(respMap);
		} catch (ClientException e) {
			Map<String, String> respMap = new LinkedHashMap<String, String>();
			respMap.put("StatusCode", "500");
			respMap.put("ErrorCode", e.getErrCode());
			respMap.put("ErrorMessage", e.getErrMsg());
			json = JSONObject.fromObject(respMap);
		}
		return json;
    }
}
